package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.coffeeseller_2.R;

import java.util.ArrayList;

/**
 * Created by liyongapple on 2018/10/11.
 */

public class KeyDisplayView extends LinearLayout {

    private int keyMargin = 24;//显示的键值标签左间距 单位dp
    private int keyClolr = Color.parseColor("#333333");
    private int keyTextSize = 40;
    private int keyLableBackgroundResource = R.drawable.shape_keydisplay_bg;
    private int count = 6; //默认标签个数
    private int keyLableSize = 70;
    private ArrayList<String> keyList = new ArrayList<>();
    private KeyCompleteListener keyCompleteListener;

    public KeyDisplayView(Context context) {
        this(context, null);
    }

    public KeyDisplayView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyDisplayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        initView();

    }

    private void initView() {
        keyList.clear();
        for (int i = 0; i < count; i++) {
            TextView keyView = new TextView(getContext());
            keyView.setBackgroundResource(keyLableBackgroundResource);
            keyView.setTextColor(keyClolr);
            keyView.setTextSize(keyTextSize);
            keyView.setGravity(Gravity.CENTER);
            LayoutParams param = new LayoutParams(dp2px(keyLableSize), dp2px(keyLableSize));
            if (i == 0) {
                param.setMargins(0, 0, 0, 0);
            } else {
                param.setMargins(dp2px(keyMargin), 0, 0, 0);
            }
            keyView.setLayoutParams(param);
            addView(keyView);
        }
    }

    public void appendKey(String keyText) {
        if (keyText == null || keyText.length() != 1) {
            return;
        }
        if (keyList.size() < getChildCount()) {

            ((TextView) getChildAt(keyList.size())).setText(keyText);
            keyList.add(keyText);
           /* for(int i=0;i<keyList.size();i++){
                ((TextView)getChildAt(i)).setText(keyList.get(i));
            }
           */
            if(keyList.size() == getChildCount() && keyCompleteListener != null){
                keyCompleteListener.onComplete();
            }
        }
    }

    public void removeLastKey() {
        if (keyList.size() > 0) {
            int lastPosition = keyList.size() - 1;
            ((TextView) getChildAt(lastPosition)).setText("");
            keyList.remove(lastPosition);
        }
    }

    public String getKeys() {
        if (keyList.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            for (String key : keyList) {
                stringBuffer.append(key);
            }
            return stringBuffer.toString();
        }
        return "";
    }

    private int dp2px(float dipValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public void setOnKeyCompleteListener(KeyCompleteListener listener) {
        keyCompleteListener = listener;
    }

    public interface KeyCompleteListener {
        void onComplete();
    }

    public KeyDisplayView setKeyMargin(int keyMargin) {
        this.keyMargin = keyMargin;
        return this;
    }

    public KeyDisplayView setKeyTextSize(int keyTextSize) {
        this.keyTextSize = keyTextSize;
        return this;
    }

    public KeyDisplayView setKeyClolr(int keyClolr) {
        this.keyClolr = keyClolr;
        return this;
    }

    public KeyDisplayView setKeyLableBackgroundResource(int keyLableBackgroundResource) {
        this.keyLableBackgroundResource = keyLableBackgroundResource;
        return this;
    }

    public KeyDisplayView setCount(int count) {
        this.count = count;
        return this;
    }

    public KeyDisplayView setKeyLableSize(int keyLableSize) {
        this.keyLableSize = keyLableSize;
        return this;
    }

    public void build() {
        removeAllViews();
        initView();
    }

}
